/*
 * @(#) LotInterService.java 2013-6-21 下午3:35:41
 *
 * Copyright 2013 Rockwell Automation, Inc. All rights reserved.
 * Rockwell Automation PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package com.osi.mes.inter.wms;


import java.util.ArrayList;
import java.util.Hashtable;
import pnuts.lang.Context;
import com.datasweep.compatibility.client.ATHandler;
import com.datasweep.compatibility.client.ATRow;
import com.datasweep.compatibility.client.Response;
import com.datasweep.compatibility.pnuts.Functions;
import com.osi.mes.inter.BaseInterService;
import com.rockwell.ftpc.framework.common.XMLUtils;
import com.rockwell.ftpc.framework.common.XmlData;

/**
 * 
 * 本接口用于和WMS同步备品备件领用单信息
 * <p>
 * 
 * @author Paul, 2013-6-21 下午3:35:57
 */
public class SparePartsInterService extends BaseInterService
{

	private static SparePartsInterService instatnce = null;

	private SparePartsInterService(Context context)
	{
		super(context);
	}

	private SparePartsInterService(Functions functions)
	{
		super(functions);
	}

	public static SparePartsInterService getInstance(Context context)
	{
		if (instatnce == null)
		{
			synchronized (SparePartsInterService.class)
			{
				if (instatnce == null)
				{
					instatnce = new SparePartsInterService(context);
				}
			}
		}
		return instatnce;
	}

	public String[] receiveSpartParts(String xmlStr)
	{
		String[] results = new String[2];
		String result = "0";
		String remark = "";
		try
		{
			System.out.println(xmlStr);
			XmlData xmlData = XMLUtils.strToXMLData(xmlStr);
			Hashtable<String, ArrayList<Object>> value = xmlData.getData();
			String billNo = getTransferValue(
				value, "billNo");
			String billUser = getTransferValue(
				value, "billUser");
			String eqpNo = getTransferValue(
				value, "eqpNo");
			String partNumber = getTransferValue(
				value, "partNumber");
			String partDesc = getTransferValue(
				value, "partDesc");
			int qty = toInteger(getTransferValue(
				value, "quantity"));
			String billDate = getTransferValue(
				value, "billDate");

			Response response = createSpartPart(
				billNo, partNumber, partDesc, eqpNo, billUser, billDate, qty);
			if (response != null && response.isError())
			{
				result = "1";
				remark = response.getFirstErrorMessage();
			}

		}
		catch (Exception e)
		{
			e.printStackTrace();
			result = "1";
			remark = e.getMessage();
		}
		results[0] = "<MessageResult><Status>"+result+"</Status><LastErrMsg>"+remark+"</LastErrMsg></MessageResult>";
		System.out.println(results[0]);
		return results;
	}

	private Response createSpartPart(String billNo, String partNumber, String partDesc,
		String eqpNo, String billUser, String billDate, int qty)
	{
		Response response = null;
		try
		{
			ATHandler atHandler = getFunctions().createATHandler(
				"EMSpareParts");
			ATRow atRow = atHandler.createATRow();
			atRow.setValue(
				"bill_no", billNo);
			atRow.setValue(
				"bill_user", billUser);
			atRow.setValue(
				"objective_eqp", eqpNo);
			atRow.setValue(
				"part_number", partNumber);
			atRow.setValue(
				"part_description", partDesc);
			atRow.setValue(
				"quantity", qty);
			atRow.setValue(
				"bill_date", getFunctions().createTime(
					billDate));
			atRow.setValue(
				"state", 0);
			response = atHandler.save();

		}
		catch (Exception e)
		{
			response = createErrorResponse(e);
		}
		return response;
	}
}
